﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DoctorWorld
{
    class Ficha
    {
        private const int POSNOMBRE = 0;
        private const int POSAPELLIDO = 1;
        private const int POSTIPODOC = 2;
        private const int POSNUMDOC = 3;
        private const int POSDOMICILIO = 4;
        private const int POSTELEFONO = 5;
        private const int POSCELULAR = 6;
        private const int POSDIA = 7;
        private const int POSMES = 8;
        private const int POSANO = 9;
        private const int POSSEXO = 10;
        private const int POSOBRASOCIAL = 11;
        private const int POSNOS = 12;
        private const int POSNHC = 13;
        private const int POSOBS = 14;
        private const String NOFILE = "noFile"; //representa que no hay archivo de ficha cargado

        private String nombre;
        private String apellido;
        private String tipoDoc;
        private String numDoc;
        private String domicilio;
        private String telefono;
        private String celular;
        private String dia;
        private String mes;
        private String ano;
        private String sexo;
        private String obraSocial;
        private String nOS;
        private String nHC;
        private String obs;

        private String pathFile;
        
        public Ficha(List<String> listaAtributos)
        {
            this.pathFile = NOFILE;
            this.nombre = listaAtributos.ElementAt(POSNOMBRE);
            this.apellido = listaAtributos.ElementAt(POSAPELLIDO);
            this.tipoDoc = listaAtributos.ElementAt(POSTIPODOC);
            this.numDoc = listaAtributos.ElementAt(POSNUMDOC);
            this.domicilio = listaAtributos.ElementAt(POSDOMICILIO);
            this.telefono = listaAtributos.ElementAt(POSTELEFONO);
            this.celular = listaAtributos.ElementAt(POSCELULAR);
            this.dia = listaAtributos.ElementAt(POSDIA);
            this.mes = listaAtributos.ElementAt(POSMES);
            this.ano = listaAtributos.ElementAt(POSANO);
            this.sexo = listaAtributos.ElementAt(POSSEXO);
            this.obraSocial = listaAtributos.ElementAt(POSOBRASOCIAL);
            this.nOS = listaAtributos.ElementAt(POSNOS);
            this.nHC = listaAtributos.ElementAt(POSNHC);
            this.obs = listaAtributos.ElementAt(POSOBS);
        }

        public Ficha()
        {
            this.pathFile = NOFILE;
            this.nombre = "";
            this.apellido = "";
            this.tipoDoc = "";
            this.numDoc = "";
            this.domicilio = "";
            this.telefono = "";
            this.celular = "";
            this.dia = "";
            this.mes = "";
            this.ano = "";
            this.sexo = "";
            this.obraSocial = "";
            this.nOS = "";
            this.nHC = "";
            this.obs = "";
        }

        public String Nombre
        {
            get { return this.nombre; }
            set { this.nombre = value; }
        }

        public String Apellido
        {
            get { return this.apellido; }
            set { this.apellido = value; }
        }

        public String TipoDoc
        {
            get { return this.tipoDoc; }
            set { this.tipoDoc = value; }
        }

        public String NumDoc
        {
            get { return this.numDoc; }
            set { this.numDoc = value; }
        }

        public String Domicilio
        {
            get { return this.domicilio; }
            set { this.domicilio = value; }
        }

        public String Telefono
        {
            get { return this.telefono; }
            set { this.telefono = value; }
        }

        public String Celular
        {
            get { return this.celular; }
            set { this.celular = value; }
        }

        public String DiaNacimiento
        {
            get { return this.dia; }
            set { this.dia = value; }
        }

        public String MesNacimiento
        {
            get { return this.mes; }
            set { this.mes = value; }
        }

        public String AnoNacimiento
        {
            get { return this.ano; }
            set { this.ano = value; }
        }

        public String Sexo
        {
            get { return this.sexo; }
            set { this.sexo = value; }
        }

        public String ObraSocial
        {
            get { return this.obraSocial; }
            set { this.obraSocial = value; }
        }

        public String NumObraSocial
        {
            get { return this.nOS; }
            set { this.nOS = value; }
        }

        public String NumHistoriaClinica
        {
            get { return this.nHC; }
            set { this.nHC = value; }
        }

        public String Observaciones
        {
            get { return this.obs; }
            set { this.obs = value; }
        }

        public String FilePath
        {
            get { return this.pathFile; }
        }

        public List<String> GetAtributos()
        {
            List<String> aux = new List<String>();
            aux.AddRange(new String[] {
            this.nombre,
            this.apellido,
            this.tipoDoc,
            this.numDoc,
            this.domicilio,
            this.telefono,
            this.celular,
            this.dia,
            this.mes,
            this.ano,
            this.sexo,
            this.obraSocial,
            this.nOS,
            this.nHC,
            this.obs
            });

            return aux;
        }

        public void BuildFromFile(String dirFile)
        {
            Parser lector = new Parser(dirFile);
            List<String> atributos = lector.GetFile();
            
            this.pathFile = dirFile;
            this.nombre = atributos.ElementAt(POSNOMBRE);
            this.apellido = atributos.ElementAt(POSAPELLIDO);
            this.tipoDoc = atributos.ElementAt(POSTIPODOC);
            this.numDoc = atributos.ElementAt(POSNUMDOC);
            this.domicilio = atributos.ElementAt(POSDOMICILIO);
            this.telefono = atributos.ElementAt(POSTELEFONO);
            this.celular = atributos.ElementAt(POSCELULAR);
            this.dia = atributos.ElementAt(POSDIA);
            this.mes = atributos.ElementAt(POSMES);
            this.ano = atributos.ElementAt(POSANO);
            this.sexo = atributos.ElementAt(POSSEXO);
            this.obraSocial = atributos.ElementAt(POSOBRASOCIAL);
            this.nOS = atributos.ElementAt(POSNOS);
            this.nHC = atributos.ElementAt(POSNHC);

            try
            {
                for (int i = POSOBS; i < atributos.Count; i++)
                    this.obs += atributos.ElementAt(i) + "\n";
                this.obs.TrimEnd();
            }
            catch (Exception e)
            { return; }
        }

        public void saveToFile(String path, String name, String extension)
        {
            this.pathFile = path + name + extension;
            String fullPath = path + name + extension;
            Writer writer = new Writer(fullPath);
            writer.BuildFile(new List<String> {
            this.nombre,
            this.apellido,
            this.tipoDoc,
            this.numDoc,
            this.domicilio,
            this.telefono,
            this.celular,
            this.dia,
            this.mes,
            this.ano,
            this.sexo,
            this.obraSocial,
            this.nOS,
            this.nHC,
            this.obs
            });
        }
    }
}
